import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
import Vuex from 'vuex';
Vue.use(Vuex);
//没有那一个接口了，这一步无法进行了
// import VueResource from 'vue-resource'
// Vue.use(VueResource);

//导入时间插件
import moment from 'moment';

import '../../lib/mui/css/mui.min.css';
import '../../lib/mui/css/icons-extra.css'

import app from '../app.vue';
// import comment from '../../components/subcomponents/comment.vue'

//这样有bug,不能实现图片的懒加载，只好换成导入全部的了
/* import { Header, Swipe, SwipeItem,Button } from 'mint-ui';

Vue.component(Header.name,Header);
Vue.component(Swipe.name,Swipe);
Vue.component(SwipeItem.name,SwipeItem);
Vue.component(Button.name,Button); */
import mintUi from 'mint-ui';
Vue.use(mintUi);

//这个缩略图真是深坑啊  我真是醉了
import VuePreview from 'vue-preview'
Vue.use(VuePreview)

import router from './router.js';

//localStorage模拟存取数据
//存取news的数据,存取之后为了不重新获取就注释了
/*var obj = [
  {id:1,src:'../../src/images/shuijiao.jpg',date:new Date(),clickNum:0,msg:'去库存'},
  {id:2,src:'../../src/images/muwu.jpg',date:new Date(),clickNum:1,msg:'去看车'},
  {id:3,src:'../../src/images/yuantiao.jpg',date:new Date(),clickNum:0,msg:'幸福'},
  {id:4,src:'../../src/images/shuijiao.jpg',date:new Date(),clickNum:0,msg:'快乐'},
  {id:5,src:'../../src/images/muwu.jpg',date:new Date(),clickNum:0,msg:'美满'},
  {id:6,src:'../../src/images/yuantiao.jpg',date:new Date(),clickNum:0,msg:'欢乐'},
  {id:7,src:'../../src/images/shuijiao.jpg',date:new Date(),clickNum:0,msg:'啦啦'},
  {id:8,src:'../../src/images/shuijiao.jpg',date:new Date(),clickNum:0,msg:'啦啦'},
  {id:9,src:'../../src/images/shuijiao.jpg',date:new Date(),clickNum:0,msg:'啦啦'},
  {id:10,src:'../../src/images/shuijiao.jpg',date:new Date(),clickNum:0,msg:'啦啦'},
  {id:11,src:'../../src/images/yuantiao.jpg',date:new Date(),clickNum:0,msg:'不编了'},
]
localStorage.setItem('news',JSON.stringify(obj));*/
//存取comment的数据,存取之后为了不重新获取就注释了,假数据就三组吧，为了那个加载评论
/* var comment1 = [
  {id:1,msg:'123',date:new Date()},
  {id:2,msg:'456',date:new Date()},
  {id:3,msg:'789',date:new Date()},
  {id:4,msg:'10,11,11',date:new Date()},
  {id:5,msg:'我想学前端',date:new Date()},
  {id:6,msg:'前端真好玩',date:new Date()},
  {id:7,msg:'我喜欢前端',date:new Date()}
];
var comment2 = [
  {id:8,msg:'10,11,11',date:new Date()},
  {id:9,msg:'我想学前端',date:new Date()},
  {id:10,msg:'前端真好玩',date:new Date()},
  {id:11,msg:'我喜欢前端',date:new Date()},
  {id:12,msg:'123',date:new Date()},
  {id:13,msg:'456',date:new Date()},
  {id:14,msg:'789',date:new Date()}
];
var comment3 = [
  {id:15,msg:'10,11,11',date:new Date()},
  {id:16,msg:'我想学前端',date:new Date()},
  {id:17,msg:'前端真好玩',date:new Date()},
  {id:18,msg:'我喜欢前端',date:new Date()},
  {id:19,msg:'123',date:new Date()},
  {id:20,msg:'456',date:new Date()},
  {id:21,msg:'789',date:new Date()}
];
localStorage.setItem('comment1',JSON.stringify(comment1))
localStorage.setItem('comment2',JSON.stringify(comment2))
localStorage.setItem('comment3',JSON.stringify(comment3)) */

//设置全局过滤器
Vue.filter('dataFor',function (data,pattern="YYYY-MM-DD hh:mm:ss") {
  return moment(data).format(pattern);
})
//设置全局评论组件 这样可以，但是视频上没有这样用，按视频上来吧
//Vue.component('comment',comment);
//设置vuex
var car = localStorage.getItem('car') || '[]'; 
var store = new Vuex.Store({
  state:{
    car:JSON.parse(car)
  },
  mutations:{
    addToCar(state,goodsinfo){
      var flag = false
      state.car.some(item=>{
        if(item.id == goodsinfo.id)
        { 
          item.count += parseInt(goodsinfo.count)
          flag = true
          return true
        }
      })
      if(!flag){
        state.car.push(goodsinfo);
      }
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    updateGoodsInfo(state, goodsinfo) {
      // 修改购物车中商品的数量值
      // 分析： 
      console.log(goodsinfo)
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.count = parseInt(goodsinfo.count)
          return true
        }
      })
      // 当修改完商品的数量，把最新的购物车数据，保存到 本地存储中
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    updateGoodsSelected(state,goodsinfo){
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.selected = goodsinfo.selected
          return true
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    removeGoods(state,goodsinfo){
      state.car.some((item,index) => {
        if (item.id == goodsinfo.id) {
          this.state.car.splice(index,1)
          return true
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    }
  },
  getters:{
    getAllCount(state){
      var c = 0
      state.car.forEach(item=>{
        if(item.selected === true)
        c += item.count
      })
      return c
    },
    getGoodsSelected(state) {
      var o = {}
      state.car.forEach(item=>{
        o[item.id] = item.selected
      })
      return o
    },
    getGoodsPrice(state){
      var p = 0;
      state.car.forEach(item=>{
        if(item.selected===true)
          p +=item.price*item.count;
      })
      return p;
    }
    
    
  }
})
var vm = new Vue({
  el:'#app',
  data:{

  },
  methods:{

  },
  render:c=>c(app),
  router,
  store
})